﻿// Microsoft Public License (Ms-PL)
// nRoute v5 (http://nroute.codeplex.com)
// Modifié par So@t

using System.Windows;
using System.Windows.Interactivity;

namespace CPN.PJN.Silverlight.UI.Interactions.Triggers
{
    public class ValueNullTrigger : TriggerBase<FrameworkElement>
    {

        public static readonly DependencyProperty SourceProperty =
            DependencyProperty.Register("Source", typeof(object), typeof(ValueNullTrigger),
            new PropertyMetadata(null, new PropertyChangedCallback(OnSourceChanged)));

        #region Properties

        public object Source
        {
            get { return GetValue(SourceProperty); }
            set { SetValue(SourceProperty, value); }
        }

        public bool Negate { get; set; }

        #endregion

        #region Handlers

        static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((ValueNullTrigger)d).CompareValue(e.NewValue);
        }

        void CompareValue(object value)
        {
            var _result = (value == null);
            if (Negate) _result = !_result;
            if (_result)
                base.InvokeActions(value);
        }

        #endregion

    }
}
